--[[
FilePath: \Lua\View\UIFuncUnlock\UIFuncUnlock.lua
功能解锁
--]]

require 'View/UIBase/UIBase'

UIFuncUnlock = UIBase.New(PanelNames.UIFuncUnlock)
local _M = UIFuncUnlock

_M.hasInit = false

function _M.Init(stageId)
    _M.stageId = stageId;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("btnBack"), self.OnBack);
        self.title = self:GetChild("n0"):GetChild("title");
        self.list = self:GetChild("list");
        self.list.itemRenderer = self.rendererUnLockItem;
        self.list:SetVirtual();

        self.listStory = self:GetChild("list2");
        self.listStory.itemRenderer = self.rendererStoryItem;
        self.listStory:SetVirtual();

        _M.hasInit = true

        self:addEvent()
    end
    self:initView();
end

function _M:addEvent()

end

function _M:removeEvent()

end

function _M:initView()
    self.title.text = getLanguage("UnlockFunctionTitle2");
    self.serverOpenDay = math.ceil((HelperFunc.GetServerTime() - logicMgr.ActiveManager.getDataUpateTime(tonumber(tostring((logicMgr.LoginManager.GetServerOpenTime()/1000)))))/86400);
    self.curMapId = logicMgr.MapManager.getMapId();
    self.unLockData = logicMgr.TaskManager.getUnlockData()
    self.list.numItems = #self.unLockData;
    self.unLockStoryData = logicMgr.TaskManager.getUnlockStoryData()
    self.listStory.numItems = #self.unLockStoryData;

    self:scrollUnlockList()
    self:scrollUnlockStoryList()

    --只显示一次的功能解锁红点
    local pref=GetPlayerPrefs("UnlockRedDot")
    if pref==nil or tonumber(pref)~=0 then
        SetPlayerPrefs("UnlockRedDot",0)
        FireEvent(Config.EventType.UnlockDesc)
    end
end

function _M:scrollUnlockList()
    local mapId = logicMgr.MapManager.getMapId();
    if _M.stageId ~= nil then
        mapId = tonumber(_M.stageId);
    end
    local index=1
    for i,v in ipairs(self.unLockData)do
        if v.f_UnlockStage>mapId then
            index=i
            break
        end
    end
    index=index-1
    if index<0 then
        index=0
    end
    --self.list:ScrollToView(#self.unLockData-1)
    --HelperFunc.ListTranstionPlay(self.list,function()
    --    --self.list:ScrollToView(index)
    --end)
    self.list:ScrollToView(index)
end

function _M:scrollUnlockStoryList()
    local mapId = logicMgr.MapManager.getMapId();
    if _M.stageId ~= nil then
        mapId = tonumber(_M.stageId);
    end
    local index=1
    for i,v in ipairs(self.unLockStoryData)do
        if v.f_UnlockStage>mapId then
            index=i
            break
        end
    end
    index=index-1
    if index<0 then
        index=0
    end
    self.listStory:ScrollToView(index)
end

function _M.rendererUnLockItem(idx, obj)
    local index = idx + 1;
    local data = _M.unLockData[index]
    local lockCtrl = obj:GetController("lock");
    local rewardCtrl = obj:GetController("haveReward");
    local stageText = obj:GetChild("Text_UnlockStage");
    local unlockStageTips = obj:GetChild("Text_UnlockStageTips");
    local unlockFuncText = obj:GetChild("Text_UnlockFunc");
    local infoText=obj:GetChild("Text_Info")
    local typeCtrl = obj:GetController("type");



    --local stageData=G.dataTable["t_stage"][tostring(data.f_UnlockStage) ]
    --stageText.text = stageData.f_ChapterId .. "-" .. stageData.f_StageNum;
    unlockFuncText.text = getLanguage("StageFunctionText"..data.f_FunctionId)
    infoText.text=getLanguage("StageFunctionInfoText"..data.f_FunctionId)
    local isUnlock=logicMgr.FuncOpenManager.isOpen(data.f_FunctionId)
    if data.f_FunctionId == 1110 or data.f_FunctionId == 613 then
        local a = 1;
    end

    typeCtrl.selectedIndex = (tonumber(data.f_FunctionType) - 1);
    if isUnlock then
        --当前解锁
        lockCtrl:SetSelectedIndex(0)
    else
        --未解锁
        lockCtrl:SetSelectedIndex(2)
        local serverOpenTime = data.f_UnlockTime or 0;
        local txtMap = "";
        if serverOpenTime < 1 or _M.serverOpenDay - 1 >= serverOpenTime then
            serverOpenTime = 0;
        else
            local ot = tonumber(tostring((logicMgr.LoginManager.GetServerOpenTime() / 1000)))
            local t = tonumber(os.date("%H", ot))
            local updateTime = G.dataTable["t_global"][tostring(35)].f_int_value;
            local openTime = 0

            if t * 3600 >= updateTime then
                openTime = logicMgr.ActiveManager.getDataUpateTime(ot) + serverOpenTime * 3600 * 24 - HelperFunc.GetServerTime();
            else
                openTime = logicMgr.ActiveManager.getDataUpateTime(ot + 3600 * 24) + serverOpenTime * 3600 * 24 - HelperFunc.GetServerTime();
            end
            serverOpenTime = math.ceil(openTime / (3600 * 24));
        end
        if _M.curMapId < data.f_UnlockStage then
            -- 关卡限制
            local stageData = G.dataTable["t_stage"][tostring(data.f_UnlockStage)];
            txtMap = stageData.f_ChapterId .. "-" .. stageData.f_StageNum;
        end
        if serverOpenTime > 0 and txtMap ~= "" then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked3"), txtMap, serverOpenTime);
        elseif serverOpenTime > 0 then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked2"), serverOpenTime);
        elseif txtMap ~= "" then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked"), txtMap);
        else
            unlockStageTips.text = "";
        end
    end
    rewardCtrl.selectedIndex=1
    local rewardList=obj:GetChild("list")
    rewardList:RemoveChildrenToPool()
    local itemCtrl = CtrlManager.GetCtrl(PanelNames.Item);
    if type(data.f_FunctionRroduction)=="table" then
        if#data.f_FunctionRroduction>0 then
            rewardCtrl.selectedIndex=0
        end
        for _,v in ipairs(data.f_FunctionRroduction)do
            local item=rewardList:AddItemFromPool()
            local itemData={}
            if logicMgr.ItemManager.IsEquip(v) then
                itemData = logicMgr.EquipManager.getEquipConstDatabyModelId(v);
            else
                itemData = logicMgr.ItemManager.getItemConstDatabyModelId(v);
            end
            --if itemData ~= nil then
            --    itemData.num = "";
            --end
            itemCtrl.initData(item, itemData);
        end
    end
end

function _M.rendererStoryItem(idx, obj)
    local index = idx + 1;
    local data = _M.unLockStoryData[index]
    local lockCtrl = obj:GetController("lock");
    local unlockStageTips = obj:GetChild("Text_UnlockStageTips");
    local unlockFuncText = obj:GetChild("Text_UnlockFunc");

    local list_Storytext = obj:GetChild("listStory")
    list_Storytext.numItems = 1
    local txt_Storytext = list_Storytext:GetChildAt(0):GetChild("Text_Info")
    txt_Storytext.text = getLanguage(data.f_FunctionStory)


    local typeCtrl = obj:GetController("type");
    unlockFuncText.text = getLanguage("StageFunctionText"..data.f_FunctionId)
    local isUnlock=logicMgr.FuncOpenManager.isOpen(data.f_FunctionId)
    typeCtrl.selectedIndex = (tonumber(data.f_FunctionType) - 1);
    if isUnlock then
        --当前解锁
        lockCtrl:SetSelectedIndex(0)
    else
        --未解锁
        lockCtrl:SetSelectedIndex(2)
        local serverOpenTime = data.f_UnlockTime or 0;
        local txtMap = "";
        if serverOpenTime < 1 or _M.serverOpenDay - 1 >= serverOpenTime then
            serverOpenTime = 0;
        else
            local ot = tonumber(tostring((logicMgr.LoginManager.GetServerOpenTime() / 1000)))
            local t = tonumber(os.date("%H", ot))
            local updateTime = G.dataTable["t_global"][tostring(35)].f_int_value;
            local openTime = 0

            if t * 3600 >= updateTime then
                openTime = logicMgr.ActiveManager.getDataUpateTime(ot) + serverOpenTime * 3600 * 24 - HelperFunc.GetServerTime();
            else
                openTime = logicMgr.ActiveManager.getDataUpateTime(ot + 3600 * 24) + serverOpenTime * 3600 * 24 - HelperFunc.GetServerTime();
            end
            serverOpenTime = math.ceil(openTime / (3600 * 24));
        end
        if _M.curMapId < data.f_UnlockStage then
            -- 关卡限制
            local stageData = G.dataTable["t_stage"][tostring(data.f_UnlockStage)];
            txtMap = stageData.f_ChapterId .. "-" .. stageData.f_StageNum;
        end
        if serverOpenTime > 0 and txtMap ~= "" then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked3"), txtMap, serverOpenTime);
        elseif serverOpenTime > 0 then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked2"), serverOpenTime);
        elseif txtMap ~= "" then
            unlockStageTips.text = string.format(getLanguage("TaskInfo_FunctionOpen_StateLocked"), txtMap);
        else
            unlockStageTips.text = "";
        end
    end
end

-- 显示
function _M:OnEnable()

end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false
end

return _M;